# coding: utf-8

import time
import pandas as pd
import asyncio
from aiohttp import ClientSession, TCPConnector

tasks = []
url = "https://www.baidu.com/{}"

allResult = []

async def request_post(url, headers,params ,sem ):
    try:
        async with sem:
            async with ClientSession(headers={'User-Agent': headers},
                                     connector=TCPConnector(limit=64, verify_ssl=False)
                                     ) as session:
                async with session.post(url,  allow_redirects=False,data=params) as response:
                        # print('Hello World:%s' % time.time())
                        print(response.status)
                        print(url)
                        time.sleep(0.1)
                        host = url.split('/')[2].split(":")[0]
                        print("response.status"+ str(response.status))
                        print("payload : "+ str(params))
                        status = str(response.status)
                        content = await response.read()
                        content_type = response.content_type
                        length = str(response.content.total_bytes)
                        resp = {}
                        resp['status'] =  str(response.status)
                        resp['content'] =  await response.read()
                        resp['url'] =  url
                        # print(response.status)
                        return resp
    except Exception as e:
        print("erro req",e)

async def request_get(url, headers,params ,sem ):
    try:
        async with sem:
            async with ClientSession(headers={'User-Agent': headers},
                                     connector=TCPConnector(limit=64, verify_ssl=False)
                                     ) as session:
                async with session.get(url,  allow_redirects=False,data=params) as response:
                        # print('Hello World:%s' % time.time())
                        print(response.status)
                        print(url)
                        time.sleep(0.1)
                        host = url.split('/')[2].split(":")[0]
                        print("response.status"+ str(response.status))
                        print("payload : "+ str(params))
                        status = str(response.status)
                        content = await response.read()
                        content_type = response.content_type
                        length = str(response.content.total_bytes)
                        resp = {}
                        resp['status'] =  str(response.status)
                        resp['content'] =  await response.read()
                        resp['url'] =  url
                        # print(response.status)
                        return resp
    except Exception as e:
        print("erro req",e)


def run():
    for i in range(5):
        task = asyncio.ensure_future(request_post(url.format(i),"dd"))
        tasks.append(task)
    result = loop.run_until_complete(asyncio.gather(*tasks))
    print(result)



def quickly():
    sem = asyncio.Semaphore(signal)  # waite 100 signal
    urls = ["https://www.baidu.com", "https://www.baidu.com"]

    for index , url in enumerate(urls):
        print("read to send req"+ str(index))
        host = url.split('/')[2].split(":")[0]
        task = asyncio.ensure_future(request_post(url,host, create_req_data(), sem))
        tasks.append(task)
    result = loop.run_until_complete(asyncio.gather(*tasks))
    print("asyncio result",result)

    for index , url in enumerate(urls):
        print("read to send req"+ str(index))
        host = url.split('/')[2].split(":")[0]
        task = asyncio.ensure_future(request_get(url,host, create_req_data(), sem))
        tasks.append(task)
    result = loop.run_until_complete(asyncio.gather(*tasks))
    print("asyncio result",result)



def to_execl(data):
    # data = [['sas', 'xcxc'], ['ss', 'ss'], ['ss', 'ss']]

    df = pd.DataFrame(data, columns=['url', 'status','content','length','host'])

    df.to_excel("company_name_li.xlsx", index=False)

def create_req_data():
    data = {
        'iphone': "aaa",
        'idCard': "sss"
    }

    return data
if __name__ == '__main__':
    signal = 500 # asyncio send request 100 signal
    loop = asyncio.get_event_loop()
    quickly()
    # run()